package tgc.edu.demo.jkxt.service;

import java.io.Console;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import ch.qos.logback.core.ConsoleAppender;
import tgc.edu.demo.jkxt.custom.BaseService;
import tgc.edu.demo.jkxt.entity.Student;
import tgc.edu.demo.jkxt.entity.SysRole;
import tgc.edu.demo.jkxt.entity.TbClass;
import tgc.edu.demo.jkxt.form.StudentForm;
import tgc.edu.demo.jkxt.repository.StudentRepository;


@Service
public class StudentService extends BaseService<Student, Integer> {
	@Autowired
	private StudentRepository studentRepository;
	@Autowired
	private SysRoleService sysRoleservice;
	

	@Value("${web.file-path}")
	private String path;


	public List<Student> findByTbClassId(Integer bjId) {
		// TODO Auto-generated method stub
		return studentRepository.findByTbClassId(bjId);
	}
	
	
	@Transactional
	public synchronized Boolean save(StudentForm form) {
		try {
			BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
			Integer id = form.getId();
			System.out.println(form.getTbClassId());
			MultipartFile file = form.getFile();
			Student model=new Student();
			if(id!=null) {
				model=findById(id);
			}else {
				model.setPassword(encoder.encode("1111"));
				List<SysRole> role=new ArrayList<SysRole>();
				role.add(sysRoleservice.findById(3));
				model.setRoles(role);
				model.setTbClass(new TbClass(form.getTbClassId()));
				
			}
			
			BeanUtils.copyProperties(form, model,"id");
			model.setUsername(model.getStuNo());
			save(model);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}
	
}
